#include "stdafx.h"

#include "SplashScreenState.h"
#include "MainMenuState.h"
#include "GameState.h"

#include "RenderSystem.h"

using namespace ZeEngine;
using namespace std;

const u32 SplashScreenState::cTimeInSplashScreenMS = 5000;

SplashScreenState::SplashScreenState() : m_splashTimer(0), m_pSplashScreenEntity(NULL)
{
}

SplashScreenState::~SplashScreenState()
{
}

bool SplashScreenState::Init(GameEngine& gameEngine)
{     
    ResourceManager& resourceManager = gameEngine.GetResourceManager();
    EntityManager& entityManager = gameEngine.GetEntityManager();

    resourceManager.AddResourceFile("res/splashscreen/resources.xml");
    resourceManager.LoadResourceGroup("splashscreen");

    const GameConfiguration& gameConfiguration = gameEngine.GetConfiguration();

    m_screenResolution = Vector2D<u32>(gameConfiguration.GetScreenWidth(), gameConfiguration.GetScreenHeight());

    SDLTexture* pTexture = resourceManager.GetResource<SDLTexture>("splash_screen_icon", "splashscreen");
    
    if (pTexture)
    {
        Vector2D<s32> textureSize = pTexture->GetDimension();
        m_pSplashScreenEntity = entityManager.CreateAndAddEntity("SplashScreen");
        m_pSplashScreenEntity->AddComponent(make_unique<SpriteComponent>(pTexture));
        m_pSplashScreenEntity->AddComponent(make_unique<TransformComponent>((m_screenResolution / 2) - (textureSize / 2)));
    }

    AddSystem(make_unique<RenderSystem>());

    return true;
}

bool SplashScreenState::Update(GameEngine& gameEngine)
{
    m_splashTimer += gameEngine.GetDeltaMS();

    if ( m_splashTimer > 1000)
    {
        gameEngine.PopState();
        gameEngine.PushState(make_unique<GameState>());
    }

    return true;
}

void SplashScreenState::Destroy(GameEngine& gameEngine)
{
    ResourceManager& resourceManager = gameEngine.GetResourceManager();
    EntityManager& entityManager = gameEngine.GetEntityManager();
    resourceManager.UnloadResourceGroup("splashscreen");
    entityManager.Clear();
}


